#ifndef NETHELPER_H
#define NETHELPER_H

#include <QHostAddress>
#include <QNetworkInterface>
#include <QHostInfo>
#include <QNetworkAddressEntry>
#include <QTcpSocket>
#include <QNetworkAccessManager>
#include <QEventLoop>
#include <QNetworkReply>


/*************************************
* 获取本机 IPV4 地址，如果有多个，返回第
* 一个有效的IPV4地址
**************************************/
QHostAddress getHostIPV4Address()
{
    foreach(const QHostAddress& hostAddress,QNetworkInterface::allAddresses())
        if ( hostAddress != QHostAddress::LocalHost && hostAddress.toIPv4Address() )
            return hostAddress;

    return QHostAddress::LocalHost;
}

QString IPV4IntegerToString(quint32 ip) {
    return QString("%1.%2.%3.%4")
            .arg((ip >> 24) & 0xFF)
            .arg((ip >> 16) & 0xFF)
            .arg((ip >> 8) & 0xFF)
            .arg(ip & 0xFF);
}

//获取主机名称
QString get_localmachine_name()
{
    QString machineName     = QHostInfo::localHostName();
    return machineName;
}

//获取MAC
/*
**
    1 "ethernet_32774" "00:FF:44:C3:C4:55" "SSTAP 1"
    2 "ethernet_32768" "80:FA:5B:37:4C:C8" "以太网"
    3 "wireless_32769" "84:EF:18:4A:24:10" "本地连接* 2"
    4 "wireless_32770" "86:EF:18:4A:24:0F" "本地连接* 3"
    5 "ethernet_32773" "84:EF:18:4A:24:13" "蓝牙网络连接"
    6 "loopback_0" "" "Loopback Pseudo-Interface 1"
    7 "wireless_32768" "84:EF:18:4A:24:0F" "WLAN"
**
*/
QString get_localmachine_mac()
{
    QList<QNetworkInterface> nets       = QNetworkInterface::allInterfaces();
    return nets.at(nets.count()-1).name();
}

void outPutContentMacInfo(){

    QList<QNetworkInterface> nets       = QNetworkInterface::allInterfaces();
    int i = 0;
    foreach(QNetworkInterface ni,nets)
    {
        i++;
        qDebug()<<i<<ni.name()<<ni.hardwareAddress()<<ni.humanReadableName();
        QList<QNetworkAddressEntry> entryList =ni.addressEntries();
        //获取IP地址条目列表，每个条目中包含一个IP地址，一个子网掩码和一个广播地址
        foreach(QNetworkAddressEntry entry,entryList)
        {
            //遍历每一个IP地址条目
        qDebug()<<"IP Address:"<<entry.ip().toString();
            //IP地址
        qDebug()<<"Netmask:"<<entry.netmask().toString();
            //子网掩码
        qDebug()<<"Broadcast:"<<entry.broadcast().toString();
            //广播地址
        }
    }
}

//判断IP地址及端口是否在线
bool IPLive(QString ip, int port) {
    QTcpSocket tcpClient;
    tcpClient.abort();
    tcpClient.connectToHost(ip, port);
    //100毫秒没有连接上则判断不在线
    return tcpClient.waitForConnected(100);
}

//获取本机公网IP地址
QString GetNetIP(QString webCode) {
    QString web = webCode.replace(" ", "");
    web = web.replace("\r", "");
    web = web.replace("\n", "");
    QStringList list = web.split("<br/>");
    QString tar = list[3];
    QStringList ip = tar.split("=");
    return ip[1];
}


//获取网页所有源代码
QString GetHtml(QString url) {
    QNetworkAccessManager *manager = new QNetworkAccessManager();
    QNetworkReply *reply = manager->get(QNetworkRequest(QUrl(url)));
    QByteArray responseData;
    QEventLoop eventLoop;
    QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
    eventLoop.exec();
    responseData = reply->readAll();
    return QString(responseData);
}



bool judgedOnline()
{
    //能接通百度IP说明可以通外网
    return IPLive("202.108.22.5", 80);
}
#endif // NETHELPER_H
